#include "FrameAbstract.h"
#include <QPainter>
#include <QMouseEvent>

FrameAbstract::FrameAbstract(FrameType type, QWidget* parent) :
	QWidget(parent),
	_type(type)
{
	setMouseTracking(true);
	_cacheImage = NULL;
}

FrameAbstract::~FrameAbstract()
{
	if(_cacheImage) delete _cacheImage;
}

void FrameAbstract::resizeEvent(QResizeEvent *event)
{
	if(_cacheImage) delete _cacheImage;
	_cacheImage = new QPixmap(size());
	_cacheImage->fill(Qt::transparent);

	QPainter painter(_cacheImage);

	paintFrame(&painter);
}

void FrameAbstract::paintEvent(QPaintEvent *event)
{
	if(_cacheImage != NULL){
		QPainter painter(this);
		painter.drawPixmap(0, 0, *_cacheImage);
	}
}

void FrameAbstract::paintFrame(QPainter* painter)
{
	painter->setRenderHint(QPainter::SmoothPixmapTransform);

	painter->setBrush(Qt::black);

	if(_type == FrameLeft){
		painter->fillRect(0,0,width(),height(),QColor(220,220,220));
		painter->drawLine(0,0,0,height());
	}
	if(_type == FrameRight){
		painter->fillRect(0,0,width(),height(),QColor(220,220,220));
		painter->drawLine(width()-1,0,width()-1,height()-1);
	}
	if(_type == FrameBottom){
		painter->fillRect(0,0,width(),height(),QColor(220,220,220));
		painter->drawLine(0,height()-1,width()-1,height()-1);
		painter->drawLine(0,0,0,height());
		painter->drawLine(width()-1,0,width()-1,height());
	}
}
